package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.bean.EditClueParam
import com.cheyun.netsalev3.bean.dms.DmsUserAuth
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import com.google.gson.Gson
import com.google.gson.JsonObject
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class ClueFollowUpRepository {
//    fun editClue(map: HashMap<String, String>, kFunction0: KFunction0<Unit>,fail:()->Unit) {
//        ApiClient.getInstance().editClue(map)
//                .subscribeOn(Schedulers.io())
//                .subscribeOn(AndroidSchedulers.mainThread())
//                .subscribe(object : ApiCallback<BaseResp<ClueDetails>>() {
//                    override fun onSuccess(t: BaseResp<ClueDetails>) {
//                        kFunction0()
//                    }
//                    override fun onError(ex: ApiException) {
//                        fail()
//                    }
//                })
//
//    }

    fun editClue(map: HashMap<String, String>, moth: (clueDetails: ClueDetails) -> Unit,fail:()->Unit) {
        ApiClient.getInstance().editClue(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<ClueDetails>>() {
                    override fun onSuccess(t: BaseResp<ClueDetails>) {
                        if(t.data == null){
                            var d = ClueDetails()
                            moth(d)
                        }else{
                            t.data?.apply(moth)
                        }
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun checkDms(uid:String , channel:String,moth: (DmsUserAuth) -> Unit,fail:()->Unit){
        ApiClient.getInstance().getDmsAuth(uid,channel)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DmsUserAuth>>() {
                    override fun onSuccess(t: BaseResp<DmsUserAuth>) {
                        t.data?.apply(moth)
                    }
                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

}